import * as types from '../constants/ActionTypes';

export default function ticket(state = [], action = {}) {
    const {
        type, payload
    } = action;
    
    switch (type) {
        case types.FETCH_TICKET:
            return { ...payload };
        case types.FETCH_TICKET_SUCCESS:
            let successInfo = null;
            if (payload.results && payload.results.length) {
                successInfo = {
                    ticket_status : 1,
                    message : "获取验证码成功"
                };
            }else{
                successInfo =  {
                    ticket_status: -1,
                    message: "获取验证码失败"
                };
            }
            return {
                ...state,
                ...successInfo
            };
        case types.FETCH_TICKET_FAILURE:
            let failureInfo = {
                ticket_status: -1,
                message: payload.message||'网络连接失败'
            };
            return {
               ...state,
               ...failureInfo
            }; 

        //=======================================================

        case types.FETCH_IMGTICKET:
            return { ...payload };
        case types.FETCH_IMGTICKET_SUCCESS:
            let imgTicketSuccessInfo = null;
            if (payload.results && payload.results.length) {
                imgTicketSuccessInfo = {
                    imgticket_status : 1,
                    message : "获取验证码成功",
                    data : payload.results[0].img
                };
            }else{
                imgTicketSuccessInfo =  {
                    imgticket_status: -1,
                    message: "获取验证码失败"
                };
            }
            return {
                ...state,
                ...imgTicketSuccessInfo
            };
        case types.FETCH_IMGTICKET_FAILURE:
            let imgTicketFailureInfo = {
                imgticket_status: -1,
                message: payload.message||'网络连接失败'
            };
            return {
               ...state,
               ...imgTicketFailureInfo
            }; 

        //=======================================================

        case types.TICKET_VALIDATE:
            return { ...payload };
        case types.TICKET_VALIDATE_SUCCESS:
            return {
                ...state,
                ...payload
            };
        case types.TICKET_VALIDATE_FAILURE:
            let failureInfo1 = {
                validate_status: -1,
                message: payload.message||'网络连接失败'
            };
            return {
               ...state,
               ...failureInfo1
            }; 
        default:
            return state;
    }
}